package com.alison.SmartCli.Utils;

import java.lang.reflect.Parameter;

public class TypeCaster {

    public static Object cast(Parameter parameter,String value){
        if (isString(parameter)){
          return value;
        } else if (isInt(parameter)){
            return Integer.valueOf(value);
        }else if (isFloat(parameter)){
            return Float.valueOf(value);
        }else if (isDouble(parameter)){
            return Double.valueOf(value);
        }else if (isLong(parameter)){
            return Long.valueOf(value);
        }else{
            return null;
        }
    }


    private static boolean isInt(Parameter parameter){
        return parameter.getType().isAssignableFrom(int.class) | parameter.getType().isAssignableFrom(Integer.class);
    }

    private static boolean isFloat(Parameter parameter){
        return parameter.getType().isAssignableFrom(float.class) | parameter.getType().isAssignableFrom(Float.class);
    }

    private static boolean isDouble(Parameter parameter){
        return parameter.getType().isAssignableFrom(double.class) | parameter.getType().isAssignableFrom(Double.class);
    }

    private static boolean isLong(Parameter parameter){
        return parameter.getType().isAssignableFrom(long.class) | parameter.getType().isAssignableFrom(Long.class);
    }

    private static boolean isString(Parameter parameter){
        return parameter.getType().isAssignableFrom(String.class);
    }

}
